<?php

class Settings extends Model {

	function Settings()
    {
        parent::Model();
    }
    
	function save()
	{
		$data = array(
			'site_name' => $this->input->post('site_name'),
			'site_description' => $this->input->post('site_description'),
			'site_lang' => $this->input->post('site_lang'),
			'site_metadata' => $this->input->post('site_metadata')
		);

		//$this->db->where('id', 1);
		
		if ($this->db->update('settings', $data))
		{
			return TRUE;
		}
		else 
		{
			return FALSE;
		}
	}
	
	// Get configs
	function get_all()
	{
		$this->db->select('*');
		$this->db->from('settings');
		$query = $this->db->get();
		
		if ($query->num_rows() > 0)
		{
			return $query->row();
		}
		else 
		{
			return FALSE;
		}

		/*$elements = directory_map(APPPATH.'elements/');
		
    	$available_elements = array();
    	
    	// Filter installable elements
    	foreach ($elements as $k => $v)
		{
			include_once (APPPATH.'elements/'.$k.'/settings.php');
			
#			if (class_exists(ucfirst($k)))
#			{
#				$element = new $k();
#			}	
			
			//$available_elements[$k] = $element;
			
			$available_elements[$k] = $settings;
		}
		
		return $available_elements;*/
	}
	
	
	function get_installed()
	{
		$this->db->select('*');
		$this->db->from('elements');
		$query = $this->db->get();
		
		return $query->result();
	}
	
	
	function get_active($remap = FALSE)
	{
		$this->db->select('*');
		$this->db->from('elements');
		$this->db->where('status', 1); 
		$query = $this->db->get();
		
		if ($remap !== FALSE)
		{
			// Remap result array indexed by column
			foreach( $query->result() as $value )
			{
				$row[$value->$remap] = $value;
			}
			return $row;
		}	
		else
		{	
			// Return standard number indexed array
			return $query->result_array();
		} 
	}
	
	function install($element)
	{
		
		$this->db->set('type', $element['type']);
		$this->db->set('name', $element['name']);
		$this->db->set('description', $element['description']);
		$this->db->set('author', $element['author']);
		$this->db->set('author', $element['version']);
		$this->db->set('params', serialize($element['config']));
		$this->db->insert('elements');
		
		//echo $this->db->affected_rows(); 
	}
	
	
	function build_element($element) // Move to lib
	{
		$element_params = unserialize($element->params);
		
		
		//var_dump($element_params);
		
		//return;
		
		return form_input($element_params); // check type
	}
	
}
?>
